package org.NooLab.itexx.app.gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.Document;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

import org.NooLab.utilities.strings.ArrUtilities;
import org.jdesktop.swingx.JXEditorPane;



/*
 *  http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html
 *  
 *  see demo class: ExampleHtmlDisplay 
 */

/**
 * 
 * 
 * 
 */
public class TransparentTextPane extends JXEditorPane{// EditorPane {
	
	Image importedImage;
	float transparency = 1.0f;
	HTMLEditorKit cssKit;
	StyleSheet styleSheet ;
	private String text;
	boolean antiAliasedText=false;
	
	
	// ========================================================================
	public TransparentTextPane(Image img, float transparency, String text) throws IOException { // 
		super();
		
		this.text = text;
		this.transparency = transparency;
		importedImage = img;
		setText(text);
		
		setOpaque(true); 
		setBackground(new Color(0f,0.0f,0f,transparency));
		
		
	}
	// ========================================================================
	
	public void enableHtml( String cssDefinitions){
		
		if ((cssDefinitions!=null) && 
			(cssDefinitions.contains("}")) && 
			(cssDefinitions.contains("{"))){
			
			
			String[] _cssrules = cssDefinitions.split("}");
			ArrayList<String> cssRules = ArrUtilities.changeArraystyle(_cssrules) ;
			
			if (cssRules.size()>0){
				setHtmlEnabled(true);
				loadStyleSheet(cssRules);
				
				Document doc = cssKit.createDefaultDocument();
				setDocument(doc);
				
				// now we can use standard setText()!
			}
			
		}
		
	}
	public void setHtmlEnabled(boolean flag){
		cssKit = new HTMLEditorKit();
		setEditorKit(cssKit);
	}
	
	public void loadStyleSheet( ArrayList<String> cssRules){
		
		styleSheet = cssKit.getStyleSheet();
		
		for (int i=0;i<cssRules.size();i++){
			String str = cssRules.get(i) ;
			if ((str.length()>0) && (str.contains("{")) && (str.contains("}")) &&
				(str.contains(";")) && (str.contains(":"))){
				
				styleSheet.addRule( str ) ;
			}
			// "body {font-family:verdana; margin: 15px; }");
		}
	}
	
	public void setPaneStyle(){
		/*
		StyledDocument doc= this.getStyledDocument();
		MutableAttributeSet attr= new SimpleAttributeSet();
		StyleConstants.setLineSpacing(attr, -0.2f); //NOTE: negative value.
		 */
	}
	
	
	
	
	// paint is executed before paintComponent ?
	//@Override
	public void _paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g.create();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));// transparency));//)); 
		/*
		 * 
		 * AlphaComposite
		 *  - SRC_IN : filled, black text hulls, area dark gray
		 *  - SRC_OVER: text not visible, image visible
		 *  - DST_OVER: text not visible, image not visible
		 *  - DST_IN : image area black
		 */
		super.paint(g2);  
	}
	
	
	public void __paint(Graphics g) {

		Graphics2D g2d = (Graphics2D) g;
	    g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING ,
	            			  RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);// LCD_HRGB);
	    							// VALUE_TEXT_ANTIALIAS_GASP);
	    /*
		g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
                			  RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
		*/
		// g2d.drawString( this.getText(), 0, 0);
		// super.paint(g2d);
	}
    /*
   */ 							 

	
    @SuppressWarnings("unchecked")
	@Override
    protected void paintComponent(Graphics g) {
    	
    	int line = 25;
    	Map<String, String> desktopHints ;
    	Graphics2D g2d = (Graphics2D) g;
    	
    	
    	
        try {
            //
            // Load an image for the background image of out JTextPane.
            // importedImage
        	
    		if (antiAliasedText){
    			

        		Image img = null;
        		Graphics2D dgc = g2d;
        		
        		GraphicsConfiguration cfg = getGraphicsConfiguration();
        		img = cfg.createCompatibleImage(getWidth(), line);
        		dgc = (Graphics2D) img.getGraphics();
        		
        		dgc.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
        							  RenderingHints.VALUE_ANTIALIAS_OFF);
        		
        		dgc.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING ,
        	            			  RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);// LCD_HRGB);

        		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        		
        		g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING ,
        	            			  RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);// LCD_HRGB);

        		
        	                	
    		}
    		  
    		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
    		
    		desktopHints = (Map<String, String>) Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints");
        	  
      	    if(desktopHints != null){
      	    	g2d.addRenderingHints(desktopHints);
      	    }

        	int w,h;
            w = (int) getSize().getWidth();
            h = (int) getSize().getHeight();
            
            g.drawImage( getImage(g), 0, 0, w,h, this);  
            
           /*
            // then paint text by ourselves ???
             
            dgc.setColor(g2d.getColor());
    		dgc.drawChars( getText().toCharArray(), 0, getText().length(), 10, line - 5);
    		g2d.drawImage(img, 0, 0, null);

    		dgc.dispose();
    		img.flush();
            */
    		
            
        } catch (Exception e) {
            e.printStackTrace();
        }
       

        super.paintComponent(g);
        
        // g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    }

    
     
	private Image getImage(Graphics g) throws IOException{
    	BufferedImage img;
    	// image = ImageIO.read(getClass().getResourceAsStream("org/NooLab/itexx/app/gui/resources/button-glassy background out.png"));
		String imgResource ="org/NooLab/itexx/app/gui/resources/button-glassy background out.png" ;
		org.NooLab.utilities.resources.ResourceLoader rL = new org.NooLab.utilities.resources.ResourceLoader();
		
		boolean hb = rL.loadImageResource( this.getClass(), imgResource) ;
		
		if (hb){
			img=null;
			img = rL.getImageResource() ;
		}else{
			Graphics2D g2d = (Graphics2D)g;
			img = g2d.getDeviceConfiguration().createCompatibleImage( (int) getSize().getWidth(),
                													  (int) getSize().getHeight(),Transparency.TRANSLUCENT);
		}
		return img;
    }

	public float getTransparency() {
		return transparency;
	}

	public void setTransparency(float transparency) {
		this.transparency = transparency;
	}
}